package com.app.xinfadi.base;

/**
 * Function: 管理APP Activity栈
 * Created on: 2015/6/23 0023 下午 14:09
 * Author: WuTengFei
 */

import android.app.Activity;

import java.util.Stack;

public class ScreenManager {
    public final static String TAG = ScreenManager.class.getSimpleName();
    private static Stack<Activity> activityStack;
    private static ScreenManager instance;

    private ScreenManager() {
    }

    public static ScreenManager getInstance() {
        if (instance == null) {
            instance = new ScreenManager();
        }
        return instance;
    }

    public void popActivity() {
        Activity activity = activityStack.lastElement();
        if (activity != null) {
            activity.finish();
        }
    }

    public void popActivity(Activity activity) {
        if (activity != null) {
            activity.finish();
            activityStack.remove(activity);
//            LogUtil.e(TAG,"pop "+activity.getClass().getSimpleName()+"  栈的数量："+getStackSize());
        }
    }

    public Activity currentActivity() {
        return activityStack.lastElement();
    }

    public void pushActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
        activityStack.add(activity);
//        LogUtil.e(TAG,"push "+activity.getClass().getSimpleName()+"  栈的数量："+getStackSize());
    }

    public void popAllActivityExceptOne(Class cls) {
        while (true) {
            Activity activity = currentActivity();
            if (activity == null) {
                break;
            }
            if (activity.getClass().equals(cls)) {
                break;
            }
            popActivity(activity);
        }
    }
    public int getStackSize(){
        if(activityStack!=null){
            return activityStack.size();
        }
        return 0;
    }
    public Stack<Activity> getActivityStack() {
        return activityStack;
    }
}
